/*
 * @Author: Bowen 849084209@qq.com
 * @Date: 2023-02-07 21:02:19
 * @LastEditors: Bowen 849084209@qq.com
 * @LastEditTime: 2023-02-09 21:03:00
 * @FilePath: \ming\ming-util\src\main\java\com\ming\ming\JGossipUtils.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package com.ming.ming;

import java.util.ArrayList;
import java.util.List;

import net.lvsq.jgossip.core.GossipService;
import net.lvsq.jgossip.core.GossipSettings;
import net.lvsq.jgossip.event.GossipListener;
import net.lvsq.jgossip.model.SeedMember;

/**
 * 
 * 集群信息发送协议封装工具类
 * 
 * @author Bowen
 *
 */
public class JGossipUtils {
	private final static String clusterName = "cluster_Ming";

	/**
	 * 创建 GossipService
	 * 
	 * @param port      当前服务端口
	 * @param cluster   当前服务名称
	 * @param ipAddress 当前服务IP
	 * @param id        当前服务编码
	 * @return 返回GossipService
	 * @throws Exception
	 */
	public static GossipService getGossipService(Integer port, String ipAddress, String id, GossipListener lst)
			throws Exception {

		// 创建节点
		List<SeedMember> seedNodes = new ArrayList<>();
		for (int i = 1; i < 6; i++) {
			SeedMember seed = new SeedMember();
			seed.setCluster(clusterName);
			seed.setIpAddress("127.0.0.1");
			seed.setPort(8080 + i);
			// seed.setId("cluster_id_808"+i);
			seedNodes.add(seed);
		}
		System.out.println("固定会员：" + seedNodes.toString());
		GossipSettings settings = new GossipSettings();
		settings.setGossipInterval(1000);

		// 创建一个 GossipService object
		try {
			GossipService gossipService = new GossipService(clusterName, ipAddress, port, id, seedNodes, settings, lst);

			return gossipService;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw e;
		}
	}
}
